/******************************************
 *  added by zlt 20230615
 *  多数据源管理
*******************************************/

#ifndef DATASTREAM_H
#define DATASTREAM_H

#include <QObject>
#include <QtSerialPort/QSerialPort>
#include <QUdpSocket>
#include <QTcpSocket>

QStringList GetPortName();//获取计算机可以用串口列表

enum CorrespondenceType
{
    NetWorkUDP_Type,
    NetWorkTCP_Type,
    Serial_Type
};

class Correspondence : public QObject
{
public:
    Correspondence(){};
    ~Correspondence(){};

    CorrespondenceType Type;

    virtual void Open() = 0;
    virtual void Close() = 0;

private:
};


/*                      串口通信                    */
class Serial : public Correspondence
{
    Q_OBJECT
public:
    Serial(
           QString SerialPort,
           QSerialPort::BaudRate Baud = QSerialPort::Baud9600,
           QSerialPort::Parity Parity = QSerialPort::NoParity,
           QSerialPort::StopBits StopBit = QSerialPort::OneStop,
           QSerialPort::DataBits DataBit = QSerialPort::Data8
           );
    ~Serial();

    void Open();
    void Close();
signals:
    void Data(QByteArray);
private slots:
    void ReceiveData();
private:
    QSerialPort * serial;
};


/*            UDP网络通信          */
class UDP : public Correspondence
{
    Q_OBJECT
public:
    UDP(quint16 port = 8080);
    ~UDP();

    void Open();
    void Close();
signals:
    void Data(QByteArray);
private slots:
    void ReceiveData();
private:
    QUdpSocket* receiver;
};


/*             TCP网络通信           */
class TCP : public Correspondence
{
    Q_OBJECT
 public:
    TCP( QString host,quint16 port);
    ~TCP();

    void Open();
    void Close();
signals:
    void Data(QByteArray);
private:
    void ReceiveData();
private:
    QString m_hostName;
    quint16 m_port;

    QTcpSocket* tcpSocket;
};


/*              网络接口通用管理        */
class DataStream : public QObject
{
    Q_OBJECT
public:
    DataStream();
    ~DataStream();
    //构建
    void CreateTcp(QString host,quint16 port);
    void CreateUdp(quint16 port = 8080);
    void CreateSerial(QString SerialPort,
                                 QSerialPort::BaudRate Baud = QSerialPort::Baud9600,
                                 QSerialPort::Parity Parity = QSerialPort::NoParity,
                                 QSerialPort::StopBits StopBit = QSerialPort::OneStop,
                                 QSerialPort::DataBits DataBit = QSerialPort::Data8);
    int Size();
    //查找
    //摧毁

    //数据处理
private slots:
    void DataProcessing(QByteArray);
private:
    QList<Correspondence *> m_Correspondence_List;
};

#endif // DATASTREAM_H
